package com.locatme.modelBD;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.locatme.model.Evenement;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Nacer
 * Date: 12/03/14
 * Time: 11:41
 * To change this template use File | Settings | File Templates.
 */
public class EvenementBD extends GestionBD {

    private long id;

    public EvenementBD(Context context) {
        super(context);
    }

    public SQLiteDatabase getBDD() {
        return db;
    }

    public long insertEvenement(Evenement even) {
        //Cration d'un ContentValues
        ContentValues values = new ContentValues();
        //on lui ajoute une valeur associe  une cl (qui est le nom de la colonne dans laquelle on veut mettre la valeur)


        values.put(BdLocatme.COL_INTITULE,even.getIntitule());
        values.put(BdLocatme.COL_DESCRIPTION_EV,even.getDescription());
        values.put(BdLocatme.COL_ADRESSE_EV,even.getAdresse());
        values.put(BdLocatme.COL_DATE_DEBUT,even.getDateDebut());
        values.put(BdLocatme.COL_DATE_FIN,even.getDateFin());
        values.put(BdLocatme.COL_H_DEBUT,even.gethDebut());
        values.put(BdLocatme.COL_H_FIN,even.gethFin());
        values.put(BdLocatme.COL_LOGIN_P_E,even.getLoginPersonne());
        //on insère l'objet dans la BDD via le ContentValues
        id = db.insert(BdLocatme.TABLE_EVENEMENT, null, values);
        return id;
    }

    public long insertinvite(int id, String login) {
        //Création d'un ContentValues
        ContentValues values = new ContentValues();
        //on lui ajoute une valeur associe  une cl (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
        values.put(BdLocatme.COL_ID_EVENEMENT_INVIT, id);
        values.put(BdLocatme.COL_LOGIN_FAVORIS_INVITE, login);
        values.put(BdLocatme.COL_INVITE_STATUT, 0);

        //on insère l'objet dans la BDD via le ContentValues
        return db.insert(BdLocatme.TABLE_INVITE, null, values);
    }

    public long getId() {
        return id;
    }

    public ArrayList<Evenement> recupEvenementCree(String login) {
        Cursor c = null;

        //Requete
        c = db.rawQuery("SELECT * FROM " + BdLocatme.TABLE_EVENEMENT + " WHERE "
                + BdLocatme.COL_LOGIN_P_E + "= ?", new String[]{login});

        return CursorToEvenement(c);
    }

    public ArrayList<Evenement> recupEvenementParticipants(String login) {
        Cursor c = null;

        //Requete
        c = db.rawQuery("SELECT * FROM " + BdLocatme.TABLE_EVENEMENT + " WHERE " + BdLocatme.COL_ID_EVENEMENT +
                " IN (SELECT " + BdLocatme.COL_ID_EVENEMENT_INVIT + " FROM " + BdLocatme.TABLE_INVITE + " WHERE "
                + BdLocatme.COL_LOGIN_FAVORIS_INVITE + "= ?)", new String[]{login});
        return CursorToEvenementInvit(c);
    }

    /**
     * CursorToSender : Parcours les rsultats contenu dans le cursor pass en
     * parametre et retourne une liste de login
     *
     * @param c : Cursor
     * @return ArrayList de login
     */
    private ArrayList<Evenement> CursorToEvenement(Cursor c) {
        //Declaration de la liste de login
        ArrayList<Evenement> event = new ArrayList<Evenement>();
        //Si le curseur n'est pas vide
        if (c.getCount() == 0 || c == null) {
            c.close();
            return null;
        }

        c.moveToFirst();
        //On parcours le curseur et on lui ajoute les login
        while (!c.isAfterLast()) {
            Evenement even = new Evenement();
            even.setId(c.getInt(0));
            even.setIntitule(c.getString(1));
            even.setDescription(c.getString(2));
            even.setAdresse(c.getString(3));
            even.setDateDebut(c.getString(4));
            even.setDateFin(c.getString(5));
            even.sethDebut(c.getString(6));
            even.sethFin(c.getString(7));
            even.setLoginPersonne(c.getString(8));
            even.setListAmis(recupInvite(c.getInt(0)));
            event.add(even);
            c.moveToNext();
        }
        c.close();
        return event;
    }

    private ArrayList<Evenement> CursorToEvenementInvit(Cursor c) {
        //Declaration de la liste de login
        ArrayList<Evenement> event = new ArrayList<Evenement>();
        //Si le curseur n'est pas vide
        if (c.getCount() == 0 || c == null) {
            c.close();
            return null;
        }

        c.moveToFirst();
        //On parcours le curseur et on lui ajoute les login
        while (!c.isAfterLast()) {
            Evenement even = new Evenement();
            even.setId(c.getInt(0));
            even.setIntitule(c.getString(1));
            even.setDescription(c.getString(2));
            even.setAdresse(c.getString(3));
            even.setDateDebut(c.getString(4));
            even.setDateFin(c.getString(5));
            even.sethDebut(c.getString(6));
            even.sethFin(c.getString(7));
            even.setLoginPersonne(c.getString(8));
            even.setListAmis(recupInvite(c.getInt(0)));
            event.add(even);
            c.moveToNext();
        }
        c.close();
        return event;
    }


    public Evenement getEvenement(int idEven) {
        Cursor c = db.rawQuery("SELECT * FROM " + BdLocatme.TABLE_EVENEMENT + " WHERE "
                + BdLocatme.COL_ID_EVENEMENT + "= ?", new String[]{Integer.toString(idEven)});


        if (c.getCount() == 0) {
            c.close();
            return null;
        }
       c.moveToFirst();
       Evenement e = new Evenement();
       e.setId(c.getInt(0));
       e.setIntitule(c.getString(1));
       e.setDescription(c.getString(2));
        e.setAdresse(c.getString(3));
       e.setDateDebut(c.getString(4));
       e.setDateFin(c.getString(5));
       e.sethDebut(c.getString(6));
       e.sethFin(c.getString(7));
       e.setLoginPersonne(c.getString(8));
        c.close();

        return e;
    }

    public void setEvenement(Evenement e) {

        db.execSQL("UPDATE "+BdLocatme.TABLE_EVENEMENT+" SET "+BdLocatme.COL_INTITULE+" = ?,"+
                BdLocatme.COL_DESCRIPTION_EV+"= ?,"+
                BdLocatme.COL_ADRESSE_EV+"= ?,"+
                BdLocatme.COL_DATE_DEBUT+"= ?,"+
                BdLocatme.COL_DATE_FIN+"= ?,"+
                BdLocatme.COL_H_DEBUT+"= ?,"+
                BdLocatme.COL_H_FIN+"= ?,"+
                BdLocatme.COL_LOGIN_P_E+"= ?"+
                " WHERE "+BdLocatme.COL_ID_EVENEMENT+" = " + e.getId(),new String[] { e.getIntitule(),e.getDescription(),e.getAdresse(),
                e.getDateDebut(),e.getDateFin(),e.gethDebut(),e.gethFin(),e.getLoginPersonne()});

    }

    public void deleteEvenement(int id) {

        db.execSQL("DELETE FROM " + BdLocatme.TABLE_EVENEMENT + " WHERE " + BdLocatme.COL_ID_EVENEMENT + " =" + id);
        db.execSQL("DELETE FROM " + BdLocatme.TABLE_INVITE + " WHERE " + BdLocatme.COL_ID_EVENEMENT_INVIT + " =" + id);
    }

    public List<String> recupInvite(int idE) {
        Cursor c = null;

        //Requete
        c = db.rawQuery("SELECT " + BdLocatme.COL_LOGIN_FAVORIS_INVITE + " FROM " + BdLocatme.TABLE_INVITE + " WHERE "
                + BdLocatme.COL_ID_EVENEMENT_INVIT + "= ?", new String[]{Integer.toString(idE)});

        return CursorToInvite(c);
    }

    public String recupInviteStatut(int idE, String login) {
        Cursor c = null;
        String s = "";
        //Requete
        c = db.rawQuery("SELECT * FROM " + BdLocatme.TABLE_INVITE + " WHERE " + BdLocatme.COL_ID_EVENEMENT_INVIT + " = " + idE + " AND " + BdLocatme.COL_LOGIN_FAVORIS_INVITE + " = '" + login + "'", null);

        if( c != null && c.moveToFirst() ){
            Log.e("myapp", String.valueOf(c.getInt(2)));
            s=String.valueOf(c.getInt(2));
            c.close();
        }
        return s;
    }

    public void setInvite(int idEvent, String login, int statut) {

        db.execSQL("UPDATE " + BdLocatme.TABLE_INVITE + " SET " + BdLocatme.COL_INVITE_STATUT + " = " + statut +
                " WHERE " + BdLocatme.COL_ID_EVENEMENT_INVIT + " = " + idEvent + " AND " + BdLocatme.COL_LOGIN_FAVORIS_INVITE + " = '" + login + "'");

    }

    private List<String> CursorToInvite(Cursor c) {
        //Declaration de la liste de login
        List<String> invite = new ArrayList<String>();
        //Si le curseur n'est pas vide
        if (c.getCount() == 0 || c == null) {
            c.close();
            return null;
        }

        c.moveToFirst();
        //On parcours le curseur et on lui ajoute les login
        while (!c.isAfterLast()) {
            invite.add(c.getString(0));
            c.moveToNext();
        }
        c.close();
        return invite;
    }
}
